package com.openatc.agent.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;


/**
 * @Classname SwaggerConfig
 * @Description
 * @Date 2022/7/5 10:23
 * @Created by panleilei
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    // 用户管理接口
    @Bean
    public Docket createUserApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.openatc.agent.controller"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(getAuthKey());
//                .groupName("所有接口");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("OpenATC-Admin")
                .description("OpenATC-Admin接口文档")
                .version("2.0")
                .build();
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = Collections.singletonList(SecurityContext.builder()
                .securityReferences(Collections.singletonList(SecurityReference.builder()
                        .reference("Authorization")
                        .scopes(new AuthorizationScope[]{new AuthorizationScope("global",
                                "Token")}).build()))
                .build());
        return securityContexts;
    }

    private List<SecurityScheme> getAuthKey() {
        return Collections.singletonList(new ApiKey("Authorization", "Authorization", "header"));
    }

}
